from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime


class MenuResponse(BaseModel):
    id: Optional[int] = Field(None, description="权限ID")
    name: str
    code: str
    desc: Optional[str] = None
    parent_id: Optional[int] = None
    type: int = Field(1, description="类型：1菜单，2按钮")
    path: Optional[str] = None
    component: Optional[str] = None
    icon: Optional[str] = None
    sort: Optional[int] = 0
    status: Optional[bool] = True
    is_hidden: Optional[bool] = False
    keep_alive: Optional[bool] = True
    is_iframe: Optional[bool] = False
    link: Optional[str] = None
    create_time: Optional[datetime] = None
    update_time: Optional[datetime] = None

    class Config:
        from_attributes = True


class MenuListResponse(BaseModel):
    total: int = Field(..., description="总数量")
    items: List[MenuResponse] = Field(..., description="菜单列表")
    page: int = Field(..., description="页码")
    size: int = Field(..., description="每页数量")
    pages: int = Field(..., description="总页数")


class RouteMeta(BaseModel):
    title: str
    icon: Optional[str] = None
    showBadge: Optional[bool] = None
    showTextBadge: Optional[str] = None
    isHide: Optional[bool] = None
    isHideTab: Optional[bool] = None
    link: Optional[str] = None
    isIframe: Optional[bool] = None
    keepAlive: Optional[bool] = None
    authList: List[dict] = Field(default_factory=list)
    isFirstLevel: Optional[bool] = None
    roles: Optional[List[str]] = None
    fixedTab: Optional[bool] = None
    activePath: Optional[str] = None
    isFullPage: Optional[bool] = None
    isAuthButton: Optional[bool] = None
    authMark: Optional[str] = None
    parentPath: Optional[str] = None


class MenuTreeNode(BaseModel):
    id: int
    name: Optional[str] = None
    code: Optional[str] = None
    type: Optional[int] = None
    path: Optional[str] = None
    component: Optional[str] = None
    meta: RouteMeta
    children: List["MenuTreeNode"] = Field(default_factory=list)

    class Config:
        arbitrary_types_allowed = True


class MenuTreeResponse(BaseModel):
    items: List[MenuTreeNode]